import dayjs from 'dayjs';
export default function generatorColor(red = random(), green = random(), blue = random(), alpha = 1) {
  return `rgba(${red}, ${green}, ${blue}, ${alpha})`;
};

function random() {
  return ~~(Math.random() * 256);
}

export const colorList = [
  // '#1C4E81',
  '#FF833B',
  '#FEC02A',
  // '#0957C3',
  '#0075E4',
  // '#3058D9',
  // '#346FF8',
  '#319CFF',
  '#22C2DA',
  // '#00B7B2',
  '#1FCAA8',
  '#7CE553',
  '#FB5F78',
  // '#FE6C83',
  '#806ACF',
  '#A560D5'
];

export const colorMap = {
  '#FF833B': [255, 131, 59],
  '#FEC02A': [254, 192, 42],
  '#0075E4': [0, 117, 228],
  '#319CFF': [49, 156, 255],
  '#22C2DA': [34, 194, 218],
  '#1FCAA8': [31, 202, 168],
  '#FB5F78': [251, 95, 120],
  '#7CE553': [124, 229, 83],
  '#806ACF': [128, 106, 207],
  '#A560D5': [165, 96, 213]
};



export const getPieChartData = (list = []) => {
  let res = list.map((e, i) => ({ ...e, itemStyle: { color: colorList[i % colorList.length], }, label: { show: true, color: colorList[i % colorList.length] } }))
  let option = {
    // title: {
    //   text: '同名数量统计',
    //   subtext: '纯属虚构',
    //   left: 'center'
    // },
    tooltip: {
      trigger: 'item',
      // formatter: '{a} <br/>{b} : {c} ({d}%)'
      formatter: '{b} : {c} ({d}%)'

    },
    // legend: {
    //   type: 'scroll',
    //   orient: 'vertical',
    //   right: 10,
    //   top: 20,
    //   bottom: 20,
    //   data: res.map(e => e.name)
    // },
    series: [
      {
        name: '',
        type: 'pie',
        radius: '55%',
        center: ['50%', '50%'],
        data: res,
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  };
  return option;
};

export const isNotNumber = (value) => {
  return typeof value !== 'number' || isNaN(value);
}

export const showNumberText = (value) => {
  // return !value ? '-' : value
  return value;
}

export const getCategoryChartData = (list = []) => {
  let res = list.map((e, i) => ({ ...e, itemStyle: { color: colorList[i % colorList.length], }, label: { show: true, color: colorList[i % colorList.length] } }))
  let option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {            // 坐标轴指示器，坐标轴触发有效
        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
      },
      formatter: function (params) {
        var tar = params[0];
        return tar.name + ' : ' + tar.value;
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    // legend: {
    //   type: 'scroll',
    //   orient: 'vertical',
    //   right: 10,
    //   top: 20,
    //   bottom: 20,
    //   data: res.map(e => e.name)
    // },
    xAxis: {
      type: 'category',
      splitLine: { show: false },
      axisLabel: {
        interval: 0,
        formatter: function (value) {
          return value.split("").join("\n");
        }
      },
      data: res.map(e => e.name)
    },
    yAxis: {
      // name: '例',
      type: 'value',
      axisLine: {
        show: true, // 显示Y轴轴线
        // 自定义轴线样式
        lineStyle: {
          // color: '#333', // 轴线颜色
          width: 1,       // 轴线宽度
          type: 'solid'   // 轴线类型：'solid', 'dashed', 'dotted'
        }
      }
    },
    series: [
      {
        // name: '',
        type: "bar",
        barWidth: 30,
        showBackground: true,
        backgroundStyle: {
          color: "transparent"
        },
        itemStyle: {
          normal: {
            barBorderRadius: [5, 5, 0, 0]
          }
        },
        label: {
          show: true,
          position: 'top',
          fontSize: 14,
          distance: 6, // 距离
        }, // 柱子上方的数值
        data: res
      }
    ]
  };

  return option;
}

export const today = dayjs().format('YYYY-MM-DD');
export const threeDayAgo = dayjs().subtract(3, 'day').format('YYYY-MM-DD');
export const sevenDayAgo = dayjs().subtract(7, 'day').format('YYYY-MM-DD');
export const twoWeekAgo = dayjs().subtract(2, 'week').format('YYYY-MM-DD');
export const monthAgo = dayjs().subtract(1, 'month').format('YYYY-MM-DD');
export const twoMonthAgo = dayjs().subtract(2, 'month').format('YYYY-MM-DD');
export const threeMonthAgo = dayjs().subtract(3, 'month').format('YYYY-MM-DD');

export const dateRange = [monthAgo, today];

export const pickerOptions = {
  shortcuts: [
    {
      text: '最近三天',
      onClick(picker) {
        picker.$emit('pick', [threeDayAgo, today]);
      }
    },
    {
      text: '最近一周',
      onClick(picker) {
        picker.$emit('pick', [sevenDayAgo, today]);
      }
    },
    {
      text: '最近两周',
      onClick(picker) {
        picker.$emit('pick', [twoWeekAgo, today]);
      }
    },
    {
      text: '最近一个月',
      onClick(picker) {
        picker.$emit('pick', [monthAgo, today]);
      }
    },
    {
      text: '最近两个月',
      onClick(picker) {
        picker.$emit('pick', [twoMonthAgo, today]);
      }
    },
    {
      text: '最近三个月',
      onClick(picker) {
        picker.$emit('pick', [threeMonthAgo, today]);
      }
    }]
};


